package com.zeroone.star.publish.controller;

import com.zeroone.star.project.vo.JsonVO;
import com.zeroone.star.publish.entity.Contact;
import com.zeroone.star.publish.entity.User;
import com.zeroone.star.publish.entity.Volunteer;
import com.zeroone.star.publish.mapper.UserMapper;
import com.zeroone.star.publish.service.impl.ContactServiceImpl;
import com.zeroone.star.publish.service.impl.RegistrationServiceImpl;
import com.zeroone.star.publish.service.impl.UserServiceImpl;
import com.zeroone.star.publish.service.impl.VolunteerServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: car-time-bank
 * @description:
 * @author: car
 * @create: 2024-04-10 16:11
 **/

@RestController
@RequestMapping("/publish/registration")
public class RegistrationController {


    @Autowired
    private RegistrationServiceImpl registrationService;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private VolunteerServiceImpl volunteerService;
    @Autowired
    private ContactServiceImpl contactService;
    @PostMapping("/enroll")
    @ApiOperation("报名")
    public JsonVO enroll(@RequestParam(value = "userId") Integer userId,
                         @RequestParam(value = "volunteerId") Integer volunteerId) {
        Integer eff = registrationService.addRegistration(userId, volunteerId);
        String message = eff == 1 ? "报名成功" : "报名失败";
        if (message.equals("报名成功")){
            Volunteer volunteer = volunteerService.selectVolunteerById(volunteerId);
            //todo:这里要加入联系人表中（如果两个人不是联系人关系的话）
            String userName = volunteer.getUserName();
            Map<String, Integer> map = userService.selectOneByName(userName);
            User user = userService.selectUserById(Long.valueOf(userId));
            Contact contact = new Contact();
            contact.setUserId(map.get(userName));
            contact.setContactId(user.getId());
            contact.setContactName(user.getName());
            contactService.addContact(contact);

            contact.setUserId(user.getId());
            contact.setContactId(map.get(userName));
            contact.setContactName(userName);
            contactService.addContact(contact);


        }
        return JsonVO.success(message);
    }

    @PostMapping("/selectAllEnrollVolunteer")
    @ApiOperation("查找报名当前活动的志愿者")
    public JsonVO selectAllEnrollVolunteer(@RequestParam(value = "volunteerId") Integer volunteerId) {
        ArrayList<Integer> userIdList = registrationService.selectAllEnrollVolunteer(volunteerId);
        List<User> users = userService.batchSelectUser(userIdList);
        return JsonVO.success(users);
    }
}
